package com.itheima.service.db.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.UmsAdmin;
import com.itheima.domain.UmsAdminRoleRelation;
import com.itheima.mapper.UmsAdminMapper;
import com.itheima.mapper.UmsAdminRoleRelationMapper;
import com.itheima.service.db.UmsAdminService;
import com.itheima.vo.PageBeanVo;
import com.mysql.jdbc.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

@DubboService
public class UmsAdminServiceImpl implements UmsAdminService {

    @Autowired
    UmsAdminMapper umsAdminMapper;

    @Autowired
    UmsAdminRoleRelationMapper umsAdminRoleRelationMapper;

    //根据用户名查询用户
    @Override
    public UmsAdmin findByOne(String username, String password) {

        LambdaQueryWrapper<UmsAdmin> qw = new LambdaQueryWrapper<>();
        qw.eq(UmsAdmin::getUsername,username).eq(UmsAdmin::getPassword, SecureUtil.md5(password));

        return umsAdminMapper.selectOne(qw);
    }

    //用户列表
    @Override
    public PageBeanVo list(Integer pageNum, Integer pageSize, String keyword) {
        Page<UmsAdmin> page = new Page<>(pageNum,pageSize);
        if(keyword ==null){
            LambdaQueryWrapper<UmsAdmin> lqw = new LambdaQueryWrapper<>();
            lqw.orderByAsc(UmsAdmin::getCreateTime);
            PageBeanVo pageBeanVo = new PageBeanVo();
            Page<UmsAdmin> pageInfo = umsAdminMapper.selectPage(page, lqw);
            //总条数
            pageBeanVo.setTotal(pageInfo.getTotal());
            pageBeanVo.setList(pageInfo.getRecords());
            pageBeanVo.setPageNum(pageNum);
            pageBeanVo.setPageSize(pageSize);
            return pageBeanVo;
        }
//        LambdaQueryWrapper<UmsAdmin> lqw1 = new LambdaQueryWrapper<>();
//        lqw1.orderByAsc(UmsAdmin::getCreateTime);
//        lqw1.like(UmsAdmin::getUsername,keyword);
//        Page<UmsAdmin> pageInfo1 = umsAdminMapper.selectPage(page, lqw1);
//
//        LambdaQueryWrapper<UmsAdmin> lqw2 = new LambdaQueryWrapper<>();
//        lqw2.orderByAsc(UmsAdmin::getCreateTime);
//        lqw2.like(UmsAdmin::getNickName,keyword);
//        Page<UmsAdmin> pageInfo2 = umsAdminMapper.selectPage(page, lqw2);
//
//        List<UmsAdmin> adminList = pageInfo1.getRecords();
//
//        if(pageInfo2.getRecords() != null){
//            for (int i = 0; i < pageInfo2.getRecords().size(); i++) {
//                adminList.add(pageInfo2.getRecords().get(i));
//            }
//        }
//        PageBeanVo pageBeanVo = new PageBeanVo();
//        //总条数
//        pageBeanVo.setTotal((long) adminList.size());
//        pageBeanVo.setList(adminList);
//        pageBeanVo.setPageNum(pageNum);
//        pageBeanVo.setPageSize(pageSize);
        return null;
    }

    @Override
    public void register(UmsAdmin umsAdmin) {
        umsAdmin.setCreateTime(LocalDateTime.now());
        umsAdminMapper.insert(umsAdmin);
    }

    @Override
    public void update(UmsAdmin umsAdmin, Long id) {
        umsAdmin.setId(id);
        umsAdmin.setCreateTime(LocalDateTime.now());
        umsAdminMapper.updateById(umsAdmin);
    }

    @Override
    public void delete(long id) {
        umsAdminMapper.deleteById(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        UmsAdmin umsAdmin = umsAdminMapper.selectById(id);
        umsAdmin.setStatus(status);
        umsAdminMapper.updateById(umsAdmin);
    }
}
